package apps;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;


public class Info {
	
	public Info(){

	}
	private static Connection con ;
	private static Statement stmt1;
	private static Statement stmt2;
	private static Statement stmt3;
	private static ResultSet rs1 ;
	private static ResultSet rs2 ;
	private static ResultSet rs3 ;
    
		
	public static void connect(){
		/*String JDBCUrl = "jdbc:oracle:thin:@136.206.35.131:1521:SSD";
        String username = "ee557";
        String password = "ee557";*/
        String JDBCUrl = "jdbc:derby://localhost:1527/Calendar;create=true";
        String username = "cal";
        String password = "cal";
        
        
        try {
            System.out.println("\nConnecting to the SSD Database......");
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            //Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(JDBCUrl, username, password);
        }
        catch (Exception e) {
            System.out.println("\nAn error has occurred during the connection phase!  This is most likely due to your CLASSPATH being set wrong and the"
                    + " Oracle classes unable to be found.  Otherwise the database itself may be down.  Try telneting to port 1521 and see if it is up!");
            e.printStackTrace();
        }
	}
	
	public static void disconnect(){
		try {    
	         if (rs1 != null) rs1.close();
	         if (stmt1 != null) stmt1.close();
	         if (rs2 != null) rs2.close();
	         if (stmt2 != null) stmt2.close();
	         if (rs3 != null) rs3.close();
	         if (stmt3 != null) stmt3.close();
	         if (con != null) con.close();
	     }
	     catch (Exception ex) {
	         System.out.println("An error occurred while closing down connection/statement"); 
           }       		    
	}

	public static Vector getInfo(String user){
		Vector returns = new Vector();
		connect();
		try {						
		     System.out.println("\nConnection Successful..... creating statement....");
	     	     stmt1 = con.createStatement();
	     	     rs1 = stmt1.executeQuery("select * from websigned_test_parties where users='"+user+"'");
	     	     while(rs1.next()){
	     	    	Vector party=new Vector();
	     	    	String id=null;
	         	    Events event=null;
	     	    	 id = rs1.getString(1);    
	     	    	stmt2 = con.createStatement();
	     	    	 rs2 = stmt2.executeQuery("select * from websigned_test_parties where event_id='"+id+"'");
	     	    	 while (rs2.next()){     	    		
	     	    		 party.add(rs2.getString(2));     	    		
	     	    	 }
	     	    	stmt3 = con.createStatement();
	     	    	rs3 = stmt3.executeQuery("select * from websigned_test_events where create_time='"+id+"'");
	     	    	while (rs3.next()){
	     	    		
	    	    	 event=new Events(rs3.getString(1),rs3.getString(2),rs3.getString(3),rs3.getString(4),party);
	    	    	 }
	     	    	
	     	    	returns.add(event);     	 	     	   
	     	     }     	         	          	   
		 }
	        catch (SQLException e) {
		     System.out.println("\nAn error has occurred during the Statement/ResultSet phase.  Please check the syntax and study the Exception details!");
	            while (e != null) {
		         System.out.println(e.getMessage());
	                e = e.getNextException();
		     }
	        }
	        disconnect();
        System.out.println("size="+returns.size());
        return returns;
	}
	
	public static boolean putInfo(Events event){
		int rs0=0;
		boolean returns=false;
		connect();
		try {
		     System.out.println("\nConnection Successful..... creating statement....");
	     	     stmt1 = con.createStatement();
	     	     System.out.println("insert into websigned_test_events values ('"+event.getCreate()+"','"+event.getStart()+"','"+event.getEnd()+"', '"+event.getDesc()+"')");
	     	     rs0 = stmt1.executeUpdate("insert into websigned_test_events values ('"+event.getCreate()+"','"+event.getStart()+"','"+event.getEnd()+"','"+event.getDesc()+"')");
	     	     
	     	    int i=0;
	     	     while(i<event.getParties().size()){
	     	    	 stmt2 =con.createStatement();
	     	    	 System.out.println("insert into websigned_test_parties values ("+event.getCreate()+",'"+event.getParties().elementAt(i)+"')");;
	     	    	 rs0=stmt2.executeUpdate("insert into websigned_test_parties values ('"+event.getCreate()+"','"+event.getParties().elementAt(i)+"')");
	     	    	    i++;	 	     	   
	     	     }    
	     	     returns=true;
		 }
	        catch (SQLException e) {
		     System.out.println("\nAn error has occurred during the Statement/ResultSet phase.  Please check the syntax and study the Exception details!");
	            while (e != null) {
		         System.out.println(e.getMessage());
	                e = e.getNextException();
		     }
	        }
	        disconnect();
	        return returns;
		
	}
	public static boolean editInfo(Events old, Events latest){
		boolean returns=false;
		if(returns=delInfo(old)==true){
			returns=putInfo(latest);
		}		
		System.out.println("done");
		return returns;		
	}
	public static boolean delInfo(Events event){
		boolean returns =false;
		int rs0 = 0;
		connect();
        try {
		     System.out.println("\nConnection Successful..... creating statement....");
	     	     stmt1 = con.createStatement();
	     	     stmt2=con.createStatement();
	     	     for (int i=0;i<event.getParties().size();i++){
	     	    	rs0 = stmt1.executeUpdate("delete from websigned_test_parties where event_id='"+event.getCreate()+"'");
	     	    	System.out.println(rs0 + ""+returns);
	     	     }
	     	     rs0 = stmt2.executeUpdate("delete from websigned_test_events where create_time='"+event.getCreate()+"'");
	     	     returns=true;
		 }
	        catch (SQLException e) {
		     System.out.println("\nAn error has occurred during the Statement/ResultSet phase.  Please check the syntax and study the Exception details!");
	            while (e != null) {
		         System.out.println(e.getMessage());
	                e = e.getNextException();
		     }         
	        }
	    System.out.println(rs0 + "ass hole"+returns);
	    disconnect();
		return returns;
		
	}
	
	public static boolean doLogin(String user, String pass){
		boolean returns=false;
		connect();		
		try {
		     System.out.println("\nConnection Successful..... creating statement....");
	     	     stmt1 = con.createStatement();
	     	     rs1 = stmt1.executeQuery("select * from websigned_test_users where users='"+user+"'");
	     	     while(rs1.next()){
	     	    	 String out =rs1.getString(2);
	     	    	 System.out.println(out.length()+" "+pass.length());
	     	    	 
	     	    	 if(out.compareToIgnoreCase(pass)==0 ){
	     	    		System.out.println(out);
	     	    		returns=true;
	     	    		break;
	     	    	 }
	     	     }
		    
	        
		 }
	        catch (SQLException e) {
		     System.out.println("\nAn error has occurred during the Statement/ResultSet phase.  Please check the syntax and study the Exception details!");
	            while (e != null) {
		         System.out.println(e.getMessage());
	                e = e.getNextException();
		     }         
	        }
	        disconnect();
	        return returns;
	}
	public static void main(String[] args){		
		/*Events event = (Events)Info.getInfo("brian").elementAt(0);
		System.out.println(Info.getInfo("bill").size()+""+event.getParties());
		
		Vector parties = new Vector();
		parties.add(new String("brian"));
		parties.add(new String("bill"));
		parties.add(new String("bob"));
		Date date = new Date();
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,3);
		
		String start = cal.getTime().toString();
		cal.add(Calendar.MINUTE, 2);
		
		String end = cal.getTime().toString();
		
		event = new Events(start,end,"party",parties);
		System.out.println(cal.getTime());
		Info.putInfo(event);
		
		Info.editInfo(event, event);
		Info.delInfo(event);*/
		System.out.println(Info.doLogin("tim", "thornton"));
	}

}